package com.biz.primus.ms.member.service;

import com.biz.primus.base.exception.BizParameterException;
import com.biz.primus.model.member.vo.MemberLevelVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.member.dao.po.MemberLevel;
import com.biz.primus.ms.member.dao.redis.MemberLevelRedisDao;
import com.biz.primus.ms.member.dao.repository.MemberLevelRepository;
import com.biz.primus.ms.member.trans.*;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * 等级
 * @author jun.liu
 * @date 2017年10月23日
 * @reviewer
 * @see
 */
@Slf4j
@Service
public class MemberLevelService extends AbstractBaseService
{
    private final MemberLevelRedisDao memberLevelRedisDao;

    private final MemberLevelRepository memberLevelRepository;

    @Autowired
    public MemberLevelService(MemberLevelRedisDao memberLevelRedisDao, MemberLevelRepository memberLevelRepository)
    {
        this.memberLevelRedisDao = memberLevelRedisDao;
        this.memberLevelRepository = memberLevelRepository;
    }

    /**
     * 根据等级id获取等级
     *
     * @return 会员等级vo数据
     * @author jun.liu
     */
    public MemberLevelVo findByIdOnPo(Long id) {
        MemberLevel po = null;
        if(id == null)
        {
            po = memberLevelRepository.findByMin(true);
        }
        else
        {
            po = memberLevelRepository.findOne(id);
        }
        if(po == null)
        {
            // todo 调试期间没有最低等级 自动建立一个 正式的时候抛出异常
            po = new MemberLevel();
            po.setLevel(1);
            po.setLevelDesc("系统自动建立的默认最低等级");
            po.setMin(true);
            saveOrUpdateUsingPoPKIncreament(memberLevelRepository, memberLevelRedisDao, po, new MemberLevelToMemberLevelRo());
            po = memberLevelRepository.findByMin(true);
//            throw new BizBusinessException("level not find");
        }
        return new MemberLevelToMemberLevelVo().apply(po);
    }

    /**
     * 获取会员等级列表
     * @return 用户等级Vo集合
     * @author jun.liu
     */
    public List<MemberLevelVo> findAllOnPo() {
        List<MemberLevel> pos = memberLevelRepository.findAll();
        return Lists.transform(pos, new MemberLevelToMemberLevelVo());
    }

    /**
     * 新增或者修改
     *
     * @param vo
     * @return
     * @author jun.liu
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(MemberLevelVo vo)
    {
        if (vo == null)
        {
            throw new BizParameterException("MemberSecretQuestionVo is null");
        }
        if (vo.getLevel() == null)
        {
            throw new BizParameterException("MemberSecretQuestionVo level Parameter is null");
        }
        MemberLevel po = new MemberLevelVoMemberLevel().apply(vo);
        if(po.getId() != null && po.getId() != 0L)
        {
            po.setUpdateTimestamp(new Timestamp(System.currentTimeMillis()));
        }
        saveOrUpdateUsingPoPKIncreament(memberLevelRepository, memberLevelRedisDao, po, new MemberLevelToMemberLevelRo());
    }

    /**
     * 删除会员等级
     * @author jun.liu
     */
    @Transactional(rollbackFor = Exception.class)
    public void delById(Long id)
    {
        if(id == null || id == 0L)
        {
            throw new BizParameterException("id Parameter is null");
        }
        this.deleteEntity(memberLevelRepository, memberLevelRedisDao, id);
    }

}
